package com.huyi.common.config;

import java.util.Map;

import javax.servlet.DispatcherType;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Maps;
import com.huyi.common.filter.XssFilter;

@Configuration
public class XssConfig {
	
    @Value("${xss.enabled}")
    private String enabled;

    @Value("${xss.excludes}")
    private String excludes;

    @Value("${xss.urlPatterns}")
    private String urlPatterns;

	/**
	 * xss过滤拦截器
	 */
	@Bean
	public FilterRegistrationBean<XssFilter> xssFilterRegistrationBean() {
		FilterRegistrationBean<XssFilter> filterRegistrationBean = new FilterRegistrationBean<XssFilter>();
		filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST);
		filterRegistrationBean.setFilter(new XssFilter());
		filterRegistrationBean.addUrlPatterns(urlPatterns.split(","));
		filterRegistrationBean.setName("xssFilter");
		filterRegistrationBean.setOrder(Integer.MAX_VALUE); // 执行顺序
		Map<String, String> initParameters = Maps.newHashMap();
        initParameters.put("excludes", excludes);
        initParameters.put("enabled", enabled);
		filterRegistrationBean.setInitParameters(initParameters);
		return filterRegistrationBean;
	}

}
